package com.song.service.impl.gxtp;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.entity.gxtp.GxtpRygx;
import com.song.entity.gxtp.GxtpRyk;
import com.song.mapper.gxtp.GxtpRykMapper;
import com.song.service.gxtp.GxtpRygxService;
import com.song.service.gxtp.GxtpRykService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 关系图谱_人员库 服务实现类
 * </p>
 *
 * @author song
 * @since 2025-01-21
 */
@Service
public class GxtpRykServiceImpl extends ServiceImpl<GxtpRykMapper, GxtpRyk> implements GxtpRykService {
    @Autowired
    GxtpRygxService gxtpRygxService;

    @Override
    public boolean checkExists(String gmsfhm) {
        LambdaQueryWrapper<GxtpRyk> lambda = new QueryWrapper<GxtpRyk>().lambda();
        lambda.eq(GxtpRyk::getGmsfhm, gmsfhm);
        GxtpRyk gxtpRyk = getOne(lambda);
        return gxtpRyk != null;
    }

    @Override
    public Map<String, Object> queryGxtp(String gmsfhm) {
        Map<String, Object> resultMap = new HashMap<>();
        LambdaQueryWrapper<GxtpRyk> lambda = new QueryWrapper<GxtpRyk>().lambda();
        lambda.eq(GxtpRyk::getGmsfhm, gmsfhm);
        GxtpRyk gxtpRyk = getOne(lambda);
        resultMap.put("center", gxtpRyk);
        List<GxtpRygx> gxtpRygxes = gxtpRygxService.queryRygxList(gmsfhm);
        if (CollectionUtil.isNotEmpty(gxtpRygxes)){
            Set<String> gmsfhms = new HashSet<>();
            for (GxtpRygx gxtpRygx : gxtpRygxes) {
                gmsfhms.add(gxtpRygx.getGmsfhmMaster());
                gmsfhms.add(gxtpRygx.getGmsfhmSlave());
            }
            LambdaQueryWrapper<GxtpRyk> nodeLambda = new QueryWrapper<GxtpRyk>().lambda();
            nodeLambda.in(GxtpRyk::getGmsfhm, gmsfhms);
            List<GxtpRyk> nodes = list(nodeLambda);
            resultMap.put("nodes", nodes);
            resultMap.put("lines", gxtpRygxes);
        }
        return resultMap;
    }
}
